using System;
using System.Windows.Forms;

namespace GeoSketch2.Core
{
    /*
     * 
     * Form qui permet de lister, modifier supprimer des points pour le redressement
     * 
     */

    public partial class ObjectPointsInput :Form
    {
        #region Fields
        private Point2D64D[] _ObjectPoints;
        private Point2D64D[] _ImagePoints;
        /// <summary>
        /// True if the coordinates are validated
        /// </summary>
        public bool Done;
        #endregion

        #region Constructor
        /// <summary>
        /// The form to input object coordinates for image rectification
        /// </summary>
        /// <param name="ImagePoints">The images coordinates of the points</param>
        public ObjectPointsInput(Point2D[] ImagePoints)
        {
            InitializeComponent();

            Done = false;

            foreach (Point2D p in ImagePoints)
            {
                dataGridView1.Rows.Add(new object[] { true, p.Id, p.Point.X, p.Point.Y, "", "" });
            }
        } 
        #endregion

        #region Events
        private void button1_Click(object sender, EventArgs e)
        {
            int nbre_pts = 0;

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if ((bool)dataGridView1.Rows[i].Cells[0].Value)
                {
                    nbre_pts++;
                }
            }

            _ObjectPoints = new Point2D64D[nbre_pts];
            _ImagePoints = new Point2D64D[nbre_pts];

            int j = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if ((bool)dataGridView1.Rows[i].Cells[0].Value)
                {
                    _ImagePoints[j] = new Point2D64D((double)dataGridView1.Rows[i].Cells[2].Value, (double)dataGridView1.Rows[i].Cells[3].Value);
                    _ObjectPoints[j] = new Point2D64D((double)Convert.ToDouble(((string)dataGridView1.Rows[i].Cells[4].Value).Replace('.', ',')), (double)Convert.ToDouble(((string)dataGridView1.Rows[i].Cells[5].Value).Replace('.', ',')));
                    j++;
                }
            }
            Done = true;
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Done = false;
            Close();
        } 
        #endregion

        #region Properties
        /// <summary>
        /// Gets the images coordinates of the points
        /// </summary>
        public Point2D64D[] ImageCoordinates
        {
            get
            {
                return _ImagePoints;
            }
        }

        /// <summary>
        /// Gets the object coordinates of the points
        /// </summary>
        public Point2D64D[] ObjectCoordinates
        {
            get
            {
                return _ObjectPoints;
            }
        } 
        #endregion
    }
}